package com.lishe.lishe.lishe.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.common.CommonUtils;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.auth.activity.AuthActivity;
import com.umeng.analytics.MobclickAgent;


/**
 * Created by lwa on 2017/8/9.
 * activity基类
 */

public class BaseActivity extends FragmentActivity {
    private boolean isFinished;
    private boolean isRunInBackground;
    public Context context;
    public boolean isResumed;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        isFinished = false;
        isRunInBackground = false;
        IntentFilter filter = new IntentFilter();
        filter.addAction(Globals.ACTION_FINISH_ALL_ACTIVITY);
        registerReceiver(mExitReceiver, filter);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //强制为横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);       //强制为竖屏
//        steepStatusBar();//沉浸模式
    }

    @Override
    public Resources getResources() {
        //固定字体大小
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    private BroadcastReceiver mExitReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Globals.ACTION_FINISH_ALL_ACTIVITY.equals(intent.getAction())) {
                context.unregisterReceiver(this);
                finish();
            }
        }
    };

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected void onPause() {
        super.onPause();
        isResumed = false;
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isResumed = true;
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    protected void onStop() {
        super.onStop();
        if (isFinished == false) {
            isRunInBackground = true;
        }
    }

    @Override
    public void finish() {
        super.finish();
        isFinished = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mExitReceiver != null) {
            try {//避免为注册时闪退
                unregisterReceiver(mExitReceiver);
            } catch (Exception e) {

            }
        }
        if (isRunInBackground == true) {

        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        isRunInBackground = false;
    }

    public void toast(String text) {
        if (!TextUtils.isEmpty(text)) {
            if (toast == null) {
                toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
            }
            toast.setText(text);
            toast.show();
        }
    }

    Toast toast;

    public void toast(int resId) {
        if (toast == null) {
            toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        }
        toast.setText(getString(resId));
        toast.show();
    }


    /**
     * 显示Fragment
     *
     * @param container
     * @param target
     */
    public void jumpFragmentReplace(int container, Fragment target) {
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(container, target).addToBackStack(null).commitAllowingStateLoss();
    }

    /**
     * 显示Fragment
     *
     * @param container
     * @param target
     */
    public void jumpFragmentAdd(int container, Fragment target) {
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().add(container, target).addToBackStack(null).commitAllowingStateLoss();
    }

    public void getDefaultProgressDialogs(String msg, boolean cancelable) {
        dialog = new ProgressDialog(context);
        dialog.setMessage(msg);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog = null;
            }
        });
        dialog.show();
    }

    public boolean dismissProgressDialogs() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
            return true;
        }
        return false;
    }

    /**
     * [沉浸状态栏]
     */
    private void steepStatusBar() {
//存在浅灰色背景层
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//             透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        }
        ////无浅灰色背景层
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            CommonUtils.changeStatusTextColor(this);
        }
    }

    protected void hideNavigationBar() {
//        View decorView = getWindow().getDecorView();
//        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_FULLSCREEN;
//        decorView.setSystemUiVisibility(uiOptions);
    }

    protected void showNavigationBar() {
//        View decorView = getWindow().getDecorView();
//        int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
//        decorView.setSystemUiVisibility(uiOptions);
    }


    /**
     * 判断是否已登录，否：跳转到 AuthActivity ，
     *
     * @return
     */
    protected void doCheckLoginIfNeedAndRedirectAuth(Activity act) {
        startActivity(new Intent(act, AuthActivity.class));
    }


    protected BaseApplication getMyApplication() {
        return (BaseApplication) (this.getApplication());
    }


    public Typeface getTypefaceRegular() {
        return getMyApplication().getTypefaceRegular();
    }

    public Typeface getTypeFaceMedium() {
        return getMyApplication().getTypeFaceMedium();
    }
}
